package com.runrunfast.face.controller;

import com.alibaba.fastjson.JSONObject;
import com.runrunfast.face.constant.YoutuUrl;
import com.runrunfast.face.service.common.YoutuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * 设备（摄像头、YouBox）Controller
 *
 * @author YangNing
 */
@Api(description = "优图")
@RestController
public class YoutuController {
    private static Logger logger = LoggerFactory.getLogger(YoutuController.class);

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    YoutuService youtuService;

    @ApiOperation("查询所有优图设备组")
    @PostMapping("/getDeviceGroups")
    public JSONObject getDeviceGroups(@RequestParam(defaultValue = "0") Integer offset, @RequestParam(defaultValue = "999") Integer rows) {
        HttpEntity requestEntity = youtuService.initEntity(null, YoutuUrl.DEVICE_GROUPS, HttpMethod.GET);
        ResponseEntity<JSONObject> response = restTemplate.exchange(YoutuUrl.HOST + YoutuUrl.DEVICE_GROUPS + "?offset={offset}&rows={rows}", HttpMethod.GET, requestEntity, JSONObject.class, offset, rows);
        return response.getBody().getJSONObject("data");
    }

    @ApiOperation("查询优图设备组详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceGroupId", paramType = "query", dataType = "String", value = "设备组id", required = true, defaultValue = "10132070")
    })
    @PostMapping("/getDeviceGroupInfo")
    public JSONObject getDeviceGroupInfo(String deviceGroupId) {
        String apiPath = YoutuUrl.DEVICE_GROUP_INFO.replace("{device-group-id}", deviceGroupId);
        HttpEntity requestEntity = youtuService.initEntity(null, apiPath, HttpMethod.GET);
        ResponseEntity<JSONObject> response = restTemplate.exchange(YoutuUrl.HOST + apiPath, HttpMethod.GET, requestEntity, JSONObject.class);
        return response.getBody().getJSONObject("data");
    }

    @ApiOperation("查询设备组回调地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceGroupId", paramType = "query", dataType = "String", value = "设备组id", required = true, defaultValue = "10132070")
    })
    @PostMapping("/getCallBackUrl")
    public JSONObject getCallBackUrl(String deviceGroupId) {
        String apiPath = YoutuUrl.DEVICE_GROUP_CALLBACK.replace("{device-group-id}", deviceGroupId);
        HttpEntity requestEntity = youtuService.initEntity(null, apiPath, HttpMethod.GET);
        ResponseEntity<JSONObject> response = restTemplate.exchange(YoutuUrl.HOST + apiPath, HttpMethod.GET, requestEntity, JSONObject.class);
        return response.getBody();
    }

    @ApiOperation("修改设备组回调地址")
    @PostMapping("/changeCallBackUrl")
    public JSONObject changeCallBackUrl(String groupId, String url) {
        String apiPath = YoutuUrl.DEVICE_GROUP_CALLBACK.replace("{device-group-id}", groupId);
        JSONObject params = new JSONObject();
        params.put("callback_url", url);
        HttpEntity requestEntity = youtuService.initEntity(params, apiPath, HttpMethod.PUT);
        ResponseEntity<JSONObject> response = restTemplate.exchange(YoutuUrl.HOST + apiPath, HttpMethod.PUT, requestEntity, JSONObject.class);
        return response.getBody();
    }

    @ApiOperation("查询所有人脸库")
    @PostMapping("/getFaceGroups")
    public JSONObject getFaceGroups(@RequestParam(defaultValue = "0") Integer offset, @RequestParam(defaultValue = "999") Integer rows) {
        HttpEntity requestEntity = youtuService.initEntity(null, YoutuUrl.FACE_GROUPS, HttpMethod.GET);
        ResponseEntity<JSONObject> response = restTemplate.exchange(YoutuUrl.HOST + YoutuUrl.FACE_GROUPS + "?offset={offset}&rows={rows}", HttpMethod.GET, requestEntity, JSONObject.class, offset, rows);
        return response.getBody().getJSONObject("data");
    }

    @ApiOperation("人脸识别绑定/解绑人脸库 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraId", paramType = "query", dataType = "String", value = "摄像头id", required = true, defaultValue = "10132614"),
            @ApiImplicitParam(name = "faceGroupId", paramType = "query", dataType = "String", value = "关联人脸库id", required = true, defaultValue = "10132073"),
            @ApiImplicitParam(name = "bindFlag", paramType = "query", dataType = "Boolean", value = "true绑定/false解绑", required = true),
            @ApiImplicitParam(name = "needCallBack", paramType = "query", dataType = "Boolean", value = "true开启/false关闭回调", required = true)
    })
    @PostMapping("/faceRecognition")
    public JSONObject faceRecognition(String cameraId, String faceGroupId, Boolean bindFlag, Boolean needCallBack) {
        String apiPath = YoutuUrl.FACE_RECOGNITION.replace("{cameras-id}", cameraId);
        JSONObject params = new JSONObject();
        params.put("face_group_id", Integer.parseInt(faceGroupId));
        params.put("flag", bindFlag);
        params.put("need_callback", needCallBack);
        HttpEntity requestEntity = youtuService.initEntity(params, apiPath, HttpMethod.PATCH);
        ResponseEntity<JSONObject> response = restTemplate.exchange(YoutuUrl.HOST + apiPath, HttpMethod.PATCH, requestEntity, JSONObject.class);
        return response.getBody();
    }

    @ApiOperation("绑定/解绑本地回调地址 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cameraId", paramType = "query", dataType = "String", value = "摄像头id", required = true, defaultValue = "10132614"),
            @ApiImplicitParam(name = "callbackUrl", paramType = "query", dataType = "String", value = "本地回调地址", required = true, defaultValue = "http://localhost:8080/callback"),
            @ApiImplicitParam(name = "bindFlag", paramType = "query", dataType = "Boolean", value = "true绑定/false解绑", required = true)
    })
    @PostMapping("/changeLocalCallback")
    public JSONObject changeLocalCallback(String cameraId, String callbackUrl, Boolean bindFlag) {
        String apiPath = YoutuUrl.DEVICE_LOCAL_CALLBACK.replace("{cameras-id}", cameraId);
        HttpMethod httpMethod = bindFlag ? HttpMethod.PUT : HttpMethod.DELETE;
        JSONObject params = new JSONObject();
        params.put("local_callback_url", callbackUrl);
        HttpEntity requestEntity = youtuService.initEntity(params, apiPath, httpMethod);
        ResponseEntity<JSONObject> response = restTemplate.exchange(YoutuUrl.HOST + apiPath, httpMethod, requestEntity, JSONObject.class);
        return response.getBody();
    }
}
